package org.zjvis.graph.analysis.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zjvis.graph.analysis.service.dto.GraphInstanceDTO;
import org.zjvis.graph.analysis.service.mapper.GraphInstanceMapper;

@Service
public class GraphInstanceService {
    @Autowired
    private GraphInstanceMapper graphInstanceMapper;


    public Long save(GraphInstanceDTO graphInstance) {
        graphInstanceMapper.save(graphInstance);
        return graphInstance.getId();
    }

    public boolean update(GraphInstanceDTO graphInstance) {
        return graphInstanceMapper.update(graphInstance);
    }

    public GraphInstanceDTO queryById(Long id) {
        return graphInstanceMapper.queryById(id);
    }

    public void setStatusRunning(GraphInstanceDTO graphInstance) {
        graphInstance.setStatus("RUNNING");
        this.update(graphInstance);
    }

    public void setStatusSuccess(GraphInstanceDTO graphInstance, Long duringTime) {
        graphInstance.setStatus("SUCCESS");
        graphInstance.setDuringTime(duringTime);
        JSONObject data = new JSONObject();
        graphInstance.setLogInfo(data.toJSONString());
        this.update(graphInstance);
    }

    public void setStatusFail(GraphInstanceDTO graphInstance, String errorMsg) {
        graphInstance.setStatus("FAIL");
        JSONObject data = new JSONObject();
        data.put("errorMsg", errorMsg);
        graphInstance.setLogInfo(data.toJSONString());
        this.update(graphInstance);
    }
}
